﻿using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_WorkshopBusiness : BaseBusiness<PB_Workshop>, IPB_WorkshopBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public PB_WorkshopBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<PB_WorkshopDTO>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            ///查询法人
            var DeptName =Db.GetIQueryable<Base_Department>().Where(m=>m.Type==1) ;
            var where = LinqHelper.True<PB_WorkshopDTO>();
            var search = input.Search;
            Expression<Func<PB_Workshop, Base_Department,Base_Department, PB_WorkshopDTO>> select = (a, b,c) => new PB_WorkshopDTO
            {
                DepartmentName = b.Name,
                WorkName=c.Name,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<Base_Department>() on a.DeptId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<Base_Department>() on a.WorpShopID equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    select @select.Invoke(a, b,c);

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_WorkshopDTO, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }
            
            where = where.And(d => _operator.Property.DeptIds.Contains(d.DeptId));
         
            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<PageResult<PB_Workshop>> GetDataListByDeptAsync(string DeptId)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_Workshop>();

            //筛选
            if (!DeptId.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_Workshop, bool>(
                    ParsingConfig.Default, false, $@"DeptId.Equals(@0)", DeptId);
                where = where.And(newWhere);
            }
            where = where.And(x=>_operator.Property.DeptIds.Contains(x.DeptId));
            return await q.Where(where).GetPageResultAsync(new PageInput());
        }

        public async Task<PageResult<PB_Workshop>> GetDataListByDepartment(string DeptId)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_Workshop>();

            //筛选
            if (!DeptId.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_Workshop, bool>(
                    ParsingConfig.Default, false, $@"DeptId.Contains(@0)", DeptId);
                where = where.And(newWhere);
            }
            where = where.And(x => _operator.Property.DeptIds.Contains(x.DeptId));
            return await q.Where(where).GetPageResultAsync(new PageInput());
        }

        public async Task<PB_Workshop> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        [DataRepeatValidate(new string[] { "Code" }, new string[] { "编号" })]
        public async Task AddDataAsync(PB_Workshop data)
        {
            data.QrCode = h_WorkStation + "," + data.Id + "," + data.DeptId+","+data.WorpShopID;
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(PB_Workshop data)
        {
            data.QrCode = h_WorkStation + "," + data.Id + "," + data.DeptId + "," + data.WorpShopID;
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}